package com.beyondbit.hr.report.support.render;

import com.beyondbit.hr.report.support.ReportRenderer;
import net.sf.jasperreports.engine.JRAbstractExporter;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.export.*;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;


public abstract class AbstractRender<E extends JRAbstractExporter> implements ReportRenderer {

    abstract public E exporter();


    public ExporterConfiguration defaultExporterConfiguration() {
        return null;
    }


    public ReportExportConfiguration defaultReportExportConfiguration() {
        return null;
    }

    @Override
    public Map<String, Object> defaultReportSettings() {
        Map<String, Object> result = new HashMap<>();
        return result;
    }

    public ExporterOutput exporterOutput(OutputStream outputStream){
        return new SimpleOutputStreamExporterOutput(outputStream);
    }

    @Override
    public void render(JasperPrint jasperPrint, ReportExportConfiguration reportExportConfiguration, ExporterConfiguration exporterConfiguration, OutputStream outputStream) throws JRException {
        E exporter = exporter();

        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(exporterOutput(outputStream));

        if (reportExportConfiguration!=null)
            exporter.setConfiguration(reportExportConfiguration);
        if (exporterConfiguration != null)
            exporter.setConfiguration(exporterConfiguration);

        exporter.exportReport();
    }
}
